package com.itHuangTao.tank.effects;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.Effect;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.almasb.fxgl.texture.Texture;
import com.itHuangTao.tank.Config;
import javafx.util.Duration;


public class HelmetEffect extends Effect {

    private AnimatedTexture texture;

    public HelmetEffect() {
        //效果存在的时间为10秒
        super(Config.HELMET_TIME);
        texture = new AnimatedTexture(
                new AnimationChannel(
                        FXGL.image("item/armed_helmet.png"),
                        Duration.seconds(1.0), 4)//1秒钟播放一帧图片，有4帧图片)
        );
    }

    //效果开始的时候
    @Override
    public void onStart(Entity entity) {
        //调整视图显示在坦克中间(加X，Y的偏移量)
        texture.setTranslateX(entity.getWidth() / 2.0 - texture.getFitWidth() / 2.0);
        texture.setTranslateY(entity.getHeight() / 2.0 - texture.getFitHeight() / 2.0);
        texture.loop();//循环播放图片帧率
        entity.getViewComponent().addChild(texture);
    }

    //效果结束的时候
    @Override
    public void onEnd(Entity entity) {
        texture.stop();//停止播放图片帧率
        entity.getViewComponent().removeChild(texture);
    }

}
